//
//  CarouselView.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "CarouselView.h"

@implementation CarouselView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];

    }
    return self;
}


- (void)createViews
{
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.itemSize = self.frame.size;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:flowlayout];
    self.collectionView.pagingEnabled = YES;

    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor  = self.backgroundColor;
    [self.collectionView registerClass:[CarouselCollectionViewCell class] forCellWithReuseIdentifier:@"CarouselCollectionViewCellIdentifier"];
    [self addSubview:self.collectionView];

    


    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 30, self.frame.size.width, 30)];
    [self addSubview:self.pageControl];
    
    
    
    

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CarouselCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarouselCollectionViewCellIdentifier" forIndexPath:indexPath];
    
    if (self.dataSourceArray.count > indexPath.row) {
        
        
        [cell.carouseImageView sd_setImageWithURL:[NSURL URLWithString:[self.dataSourceArray[indexPath.row] valueForKey:@"pic"]]];
    }
    
    

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}



- (void)whenHasDataCarouselCollectionOpeation
{
    _pageControl.numberOfPages = self.dataSourceArray.count;
    if (self.dataSourceArray.count > 1) {
        id last = [self.dataSourceArray lastObject];
        id frist = self.dataSourceArray[0];
        [self.dataSourceArray addObject:frist];
        [self.dataSourceArray insertObject:last atIndex:0];
        
        self.pageControl.numberOfPages = self.dataSourceArray.count - 2;
        self.pageControl.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    
    self.collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
    [self.collectionView reloadData];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(automaticPlay) userInfo:nil repeats:YES];
    //计时器拖动也会继续触发方法
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    
    
    
}

//定时器触发的方法

- (void)automaticPlay {
   //添加动画效果

    CATransition *animation = [CATransition animation];

    animation.duration = 1;

    [animation setType:@"suckEffect"];

    animation.subtype = kCATransitionFromRight;

    [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x + self.width, 0) animated:NO];
    [self.collectionView.layer addAnimation:animation forKey:nil];
    
    if (self.collectionView.contentOffset.x == (self.dataSourceArray.count - 1) * self.width) {
        self.collectionView.contentOffset = CGPointMake(self.width, 0);
    }
    if (self.collectionView.contentOffset.x == 0) {
        self.collectionView.contentOffset = CGPointMake((self.dataSourceArray.count - 2) * self.width, 0);
    }
    
    self.pageControl.currentPage = self.collectionView.contentOffset.x / self.width - 1;


    
    
    
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = (NSInteger)(scrollView.contentOffset.x / self.collectionView.frame.size.width);
    //KELog(@"%ld", index);
    if (index == self.dataSourceArray.count - 1) {
        self.collectionView.contentOffset =  CGPointMake(self.frame.size.width, 0);
        index = 0;
    }else if(index == 0)
    {
        self.collectionView.contentOffset =  CGPointMake((self.dataSourceArray.count - 2 )*self.frame.size.width, 0);
        index = self.dataSourceArray.count - 1;
    }
    self.pageControl.currentPage = index - 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    KELog(@"%ld", self.pageControl.currentPage);
//    self.carouseBlock(self.pageControl.currentPage + 1);
}



@end
